package com.kefu.common.result;

import com.kefu.framework.spring.KefuApplicationContext;
import org.springframework.context.support.MessageSourceAccessor;

public class ResultGenerator {
    private static final String DEFAULT_SUCCESS_MESSAGE = "成功";

    public ResultGenerator() {
    }

    public static <T> Result<T> genSuccessResult() {
        Result<T> result = new Result();
        result.setCode(ResultCode.SUCCESS.code());
        result.setMessage("成功");
        return result;
    }

    public static <T> Result<T> genSuccessResult(T data) {
        Result<T> result = new Result();
        result.setCode(ResultCode.SUCCESS.code());
        result.setData(data);
        result.setMessage("成功");
        return result;
    }

    public static <T> Result<T> genFailResult(String message) {
        Result<T> result = new Result();
        result.setCode(ResultCode.FAIL.code());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> genForbiddenResult(String message) {
        Result<T> result = new Result();
        result.setCode(ResultCode.FORBIDDEN.code());
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> genFailResult(String code, String message) {
        Result<T> result = new Result();
        result.setCode(ResultCode.FAIL.code());
        result.setMessage(((MessageSourceAccessor) KefuApplicationContext.getBean(MessageSourceAccessor.class)).getMessage(code, message));
        return result;
    }

    public static <T> Result<T> genFailResult(String code, Object[] args, String message) {
        Result<T> result = new Result();
        result.setCode(ResultCode.FAIL.code());
        result.setMessage(((MessageSourceAccessor) KefuApplicationContext.getBean(MessageSourceAccessor.class)).getMessage(code, args, message));
        return result;
    }

    public static <T> Result<T> genFailResult() {
        Result<T> result = new Result();
        result.setCode(ResultCode.FAIL.code());
        return result;
    }

    public static <T> Result<T> genValidFailResult() {
        Result<T> result = new Result();
        result.setCode(ResultCode.VALID_ERROR.code());
        return result;
    }

    public static <T> Result<T> genValidFailResult(T data) {
        Result<T> result = new Result();
        result.setCode(ResultCode.VALID_ERROR.code());
        result.setData(data);
        result.setMessage(((MessageSourceAccessor) KefuApplicationContext.getBean(MessageSourceAccessor.class)).getMessage("global.validate.invalid", "验证失败"));
        return result;
    }
}